package com.holike.crm.fragment.message;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gallopmark.recycler.adapterhelper.BaseRecyclerAdapter;
import com.gallopmark.recycler.adapterhelper.CommonAdapter;
import com.holike.crm.R;
import com.holike.crm.base.MyFragment;
import com.holike.crm.bean.MessageResultBean;
import com.holike.crm.bean.MultiItem;
import com.holike.crm.helper.RefreshAndLoadMoreHelper;
import com.holike.crm.presenter.fragment.MessageV2Presenter;
import com.holike.crm.view.fragment.MessageV2View;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;


/**
 * Created by pony on 2019/8/21.
 * Copyright holike possess 2019.
 */
public abstract class AbsMessageFragment extends MyFragment<MessageV2Presenter, MessageV2View>
        implements MessageV2View, BaseRecyclerAdapter.OnItemClickListener, RefreshAndLoadMoreHelper.Callback {

    abstract class AbsMessageAdapter extends CommonAdapter<MultiItem> {

        AbsMessageAdapter(Context context, List<MultiItem> dataList) {
            super(context, dataList);
        }

        @Override
        public int getItemViewType(int position) {
            return mDatas.get(position).getItemType();
        }

        @Override
        protected int bindView(int viewType) {
            if (viewType == 2) {
                return R.layout.item_nomore_data;
            }
            return getItemLayoutResourceId();
        }

        abstract int getItemLayoutResourceId();

        @Override
        public void onBindHolder(RecyclerHolder holder, MultiItem item, int position) {
            if (item.getItemType() == 0) {
                MessageResultBean.MessageBean bean = (MessageResultBean.MessageBean) item;
                bindHolder(holder, bean, position);
            } else {
                holder.setText(R.id.mNoMoreTextView, mContext.getString(R.string.list_no_more));
            }
        }

        abstract void bindHolder(RecyclerHolder holder, MessageResultBean.MessageBean bean, int position);
    }

    private RefreshAndLoadMoreHelper mHelper;
    private String mType = "1"; //类型 1待处理事项 2公告

    private IMessageRequestCallback mCallback;

    public AbsMessageFragment setCallback(IMessageRequestCallback callback) {
        this.mCallback = callback;
        return this;
    }

    @Override
    protected MessageV2Presenter attachPresenter() {
        return new MessageV2Presenter();
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.fragment_imessage;
    }

    @Override
    protected void init() {
        mType = getType();
//        mMessageList = new ArrayList<>();
//        mAdapter = newAdapter();
        SmartRefreshLayout refreshLayout = mContentView.findViewById(R.id.smartRefreshLayout);
        RecyclerView recyclerView = mContentView.findViewById(R.id.recyclerView);
        mHelper = new RefreshAndLoadMoreHelper(refreshLayout, 1, 20, this);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        AbsMessageAdapter adapter = newAdapter(mHelper.getListBeans());
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
        mHelper.setAdapter(adapter);
        initData();
    }

    abstract String getType();

    @NonNull
    abstract AbsMessageAdapter newAdapter(List<MultiItem> dataList);

    private void initData() {
        showLoading();
        mHelper.initData();
    }

    @Override
    public void onLoadData(int pageNo, int pageSize) {
        mPresenter.getMessageList(mType, pageNo, pageSize);
    }

//    @Override
//    public void onNoResult() {
//        if (TextUtils.equals(mType, "1")) {
//            noData(R.drawable.no_result, R.string.tips_nopendingevents, false);
//        } else {
//            noData(R.drawable.no_notice, R.string.tips_noannounce, false);
//        }
//    }

    @Override
    public void onHasData() {
        hasData();
    }

    @Override
    public void onLoadDataFailure(String failReason, boolean hide) {
        if (isNoAuth(failReason)) {
            noAuthority();
        } else {
            if (hide) {
                noNetwork(failReason);
            } else {
                showShortToast(failReason);
            }
        }
    }

    @Override
    public void onSuccess(MessageResultBean resultBean) {
        dismissLoading();
        if (mCallback != null) {
            mCallback.onResponse(resultBean);
        }
        mHelper.setResult(resultBean.getMessageList());
    }

    @Override
    public void onFailure(String failReason) {
        dismissLoading();
        mHelper.onFailure(failReason);
    }

    @Override
    protected void reload() {
        initData();
    }

    @Override
    public void onItemClick(BaseRecyclerAdapter adapter, BaseRecyclerAdapter.RecyclerHolder holder, View view, int position) {
        if (position >= 0 && position < mHelper.getListBeans().size()) {
            MultiItem item = mHelper.getListBeans().get(position);
            if (item.getItemType() == 0) {
                onItemClick((MessageResultBean.MessageBean) item, position);
            }
        }
    }

    abstract void onItemClick(MessageResultBean.MessageBean messageBean, int position);

    @Override
    public void onRedistributeSuccess(String message) {
        dismissLoading();
        showShortToast(message);
        initData();
    }

    @Override
    public void onRedistributeFailure(String failReason) {
        dismissLoading();
        showShortToast(failReason);
    }

    public interface IMessageRequestCallback {
        void onResponse(MessageResultBean bean);
    }
}
